package com.zhou.vartype

import Array._

/**
  *
  * @author 数组的积累
  * @date 2018/10/26
  */
object ArrayAccum {

  def main(args: Array[String]): Unit = {
    var arr = Array(1, 4, 7, 2)

    var total = arr(0)
    var max = arr(0)
    for (i <- 1 to (arr.length - 1)) {
      total += arr(i)
      if (arr(i) > max) {
        max = arr(i)
      }
    }
    printf("最大值=%d \t 总和=%d \n", max, total)

    //多维数组，需要导入Array包
    var matr = ofDim[Int](3, 3)
    for (i <- 0 to 2) {
      for (j <- 0 to 2) {
        matr(i)(j) = i + j
      }
    }

    for (m <- 0 to 2) {
      for (n <- 0 to 2) {
        print(matr(m)(n) + "\t")
      }
      println()
    }

    //生成区间数组
    var range_num = range(1, 20, 2)
    range_num.foreach(v => printf(v + "\t"))
    println()

    //返回指定长度数组，每个数组元素为指定函数的返回值，默认从0开始
    Array.tabulate(5)(a => a + 5).foreach(printf("%d \t", _))
    println()

    //tabulate方法需要继续研究
    var clc = (x: Int, y: Int) => x * y
    var nw = Array.tabulate(5, 5)(clc(_, _))

    //fill(m)(n)返回指定长度m的数组，以固定值n填充
    var arr_fill = fill[Int](5)(3)
    arr_fill.foreach(printf("%d" + "\t", _))
    println()
  }


}
